﻿CREATE PROC [dbo].[usp_ElectricityRealtimeInsert] 
    @cc_dateTime datetime,
    @cc_watts int,
    @cc_joules bigint,
    @cc_meterId int
AS 
	SET NOCOUNT ON 
	SET XACT_ABORT ON  
	

	
	
	
	
	IF NOT EXISTS (SELECT * FROM [ElectricityRealtime] WHERE [cc_dateTime] = @cc_dateTime AND cc_meterId =@cc_meterId )
	
		BEGIN
			PRINT 'NO DATA, INSERTING'	
			INSERT INTO [dbo].[ElectricityRealtime] ([cc_dateTime], [cc_watts], [cc_joules], [cc_meterId])
			SELECT @cc_dateTime, @cc_watts, @cc_joules, @cc_meterId
			
			-- Begin Return Select <- do not remove
			SELECT [cc_id], [cc_dateTime], [cc_watts], [cc_joules], [cc_meterId]
			FROM   [dbo].[ElectricityRealtime]
			WHERE  [cc_id] = SCOPE_IDENTITY()
	-- End Return Select <- do not remove
		END
	ELSE
		BEGIN
		PRINT 'RECORD ALREADY EXISTS'	
		SELECT * FROM [ElectricityRealtime] WHERE [cc_dateTime] = @cc_dateTime AND cc_meterId =@cc_meterId
		END


